package com.centaline.androidsalesblog.base;

import android.text.TextUtils;

import com.baidu.mobstat.StatService;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.utils.DateUtil;
import com.centanet.framework.base.AbstractActivity;
import com.centanet.framework.network.VolleyRequestQueue;

import java.util.Locale;

/**
 * Activity基础父类
 * Created by guilin on 15/12/15.
 */
public abstract class BaseActivity extends AbstractActivity {

    protected RequestManager requestManager;

    @Override
    protected void preInit() {
        requestManager = Glide.with(this);
    }

    @Override
    protected void onResume() {
        if (checkRequestManager())
            requestManager.resumeRequests();
        StatService.onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (checkRequestManager())
            requestManager.pauseRequests();
        StatService.onPause(this);
        super.onPause();
    }

    @Override
    protected void onStart() {
        if (checkRequestManager())
            requestManager.onStart();
        super.onStart();
    }

    @Override
    protected void onStop() {
        if (checkRequestManager())
            requestManager.onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (checkRequestManager())
            requestManager.onDestroy();
        VolleyRequestQueue.getRequestQueue().cancelAll(getClass());
        super.onDestroy();
    }

    @Override
    public void onTrimMemory(int level) {
        if (checkRequestManager())
            requestManager.onTrimMemory(level);
        super.onTrimMemory(level);
    }

    @Override
    public void onLowMemory() {
        if (checkRequestManager())
            requestManager.onLowMemory();
        super.onLowMemory();
    }

    /**
     * 检查requestManager是否为空
     */
    protected boolean checkRequestManager() {
        return requestManager != null;
    }

    /**
     * 百度自定义事件
     */
    protected void baiduEvent(String id) {
        baiduEvent(id, null);
    }

    /**
     * 百度自定义事件
     */
    protected void baiduEvent(String id, String label) {
        if (TextUtils.isEmpty(label)) {
            StatService.onEvent(this, id,
                    String.format(Locale.CHINA, "%s_%s",
                            AppConstant.CityCode,
                            DateUtil.format(System.currentTimeMillis(), DateUtil.FORMAT10)));
        } else {
            StatService.onEvent(this, id,
                    String.format(Locale.CHINA, "%s_%s_%s",
                            AppConstant.CityCode,
                            label,
                            DateUtil.format(System.currentTimeMillis(), DateUtil.FORMAT10)));
        }
    }
}
